<?php
/*
 * SYS操作类
 */
namespace Home\Controller;
use Think\Controller; 
class SysdoController extends Controller {
	
	public $fc = "";
	public function __construct(){
		parent::__construct();
		$this->fc = array_merge($_POST,$_GET);
		if(!Csession())echo '<script type="text/javascript">window.top.location="'.__APP__.'";</script>';
	}	
	
	//网站配置
	public function sys_settings(){
		import('@.Class.mcache');
		$System = M("System");
		$data = $this->fc;
		if($data['act']=="edit"){//编辑
			$dat['sys'] = serialize($data['sys']);
			$res = $System->where('id='.$data['id'])->save($dat);
			if($res){
				MCache::set('system',$data['sys']);
				$this->success('修改成功', $data['callback']);
			}else{
				$this->error('修改失败', $data['callback']);
			}
		}else{
			$this->error('非法操作');
		}
	}
	
	//管理员
	public function aduser(){
		$Aduser = M("Aduser");
		$data = $this->fc;
		$data['adpw'] = md5($data['adpw']);	
		$data['adprivileges'] = 1;
		if($data['act']=="add"){//新增	
			if($Aduser->add($data)){
				$this->success('新增成功', $data['callback']);
			}else{
				$this->error('新增失败');
			}
		}elseif($data['act']=="edit"){//编辑
			$res = $Aduser->where('id='.$data['id'])->save($data);
			if($res){
				$this->success('修改成功', $data['callback']);
			}else{
				$this->error('修改失败');
			}			
		}elseif($data['act']=="del"){//删除
			if($data['id']==1){
				$this->error('不能删除超级管理员');
			}
			$res = $Aduser->where('id='.$data['id'])->delete();
			if($res){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}				
		}else{
			$this->error('非法操作');
		}		
	}

	//导航
	public function nav(){	
		$Nav = M("Nav");
		$data = $this->fc;
		if($data['act']=="add"){//新增	
			if($Nav->add($data)){
				$this->nav_cache();
				$this->success('新增成功', $data['callback']);
			}else{
				$this->error('新增失败');
			}
		}elseif($data['act']=="edit"){//编辑
			$res = $Nav->where('id='.$data['id'])->save($data);
			if($res){
				$this->nav_cache();
				$this->success('修改成功', $data['callback']);
			}else{
				$this->error('修改失败');
			}			
		}elseif($data['act']=="del"){//删除
			$res = $Nav->where('id='.$data['id'])->delete();
			if($res){
				$this->nav_cache();
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}				
		}else{
			$this->error('非法操作');
		}		
	}	
	
	public function watermark(){
		$data = $_POST;
		import('@.Class.mcache');
		$MCache = new MCache();
		$MCache->init($data,"WatermarkCache","cache/admin/","watermark.cache");
		$MCache->ArrayCache();
		$this->success('修改成功');
	}
	
	public function email(){
		if(!empty($this->fc)){
			import('@.Class.mcache');
			$MCache = new MCache();
			$MCache->init($this->fc,"EmailCache","cache/admin/","email.cache");
			$MCache->ArrayCache();
			$this->success('执行成功');
		}else{
			$this->error('操作有误');
		}
	}
	
	//导航缓存
	private function nav_cache(){	
		$Nav = M("Nav");
		$Nav->table(_PRE_.'nav');
		$Nav->order('sort desc,id asc');
		$arr = $Nav->select();		
		import('@.Class.mcache');
		$MCache = new MCache();
		$MCache->init($arr,"NavCache","cache/home/","nav.cache");
		$MCache->ArrayCache();		
	}
	
	public function power(){
		$data = $this->fc;
		if(empty($data['power'])){
			$this->error('数据为空!', $data['callback']);
			return;
		}
		$data['power'] = serialize($data['power']);
		$Aduser = M("Aduser");
		$res = $Aduser->where('id='.$data['id'])->save($data);
		if($res){
			$this->success('保存成功', $data['callback']);
		}else{
			$this->error('保存失败', $data['callback']);
		}
	}
	
}

?>